<?php
$pagename = '搜索题目';
include 'header.php';
?>
		<div id="nav">
			<h1><a href="index.php">NDSOJ</a></h1>
			<ul>
				<li><a href="index.php">首页</a></li>
				<?php if(!$_SESSION['login']) echo "<li><a href=\"reg.php\">注册</a></li>\n\t\t\t\t";
				?><li><a href="problem.php">题库</a></li>
				<li><a href="contest.php">比赛</a></li>
				<li><a href="discuss.php">讨论</a></li>
				<li><a href="help.php">帮助</a></li>
				<li><a href="about.php">关于</a></li>
			</ul>
		</div>
		<div id="mainbox">
			<h2>搜索结果</h2>
			<?php
			require_once 'mysql_conn_3asY&AKj0qB1.php';
			$first = 1;
			if(is_numeric($_REQUEST['difficulty'])) {
				$difficulty = escape_data($_REQUEST['difficulty']);
				$str .= "难度：$difficulty&nbsp;&nbsp;&nbsp;&nbsp;";
				$clause .= "WHERE difficulty = $difficulty ";
				$first = 0;
			}
			if(is_numeric($_REQUEST['type'])) {
				$type = escape_data($_REQUEST['type']);
				include 'var_type.php';
				$str .= "类型：{$problem_type[$type]}&nbsp;&nbsp;&nbsp;&nbsp;";
				if($first) {
					$clause .= "WHERE type = $type ";
					$first = 0;
				}
				else
					$clause .= "AND type = $type ";
			}
			if(is_numeric($_REQUEST['source'])) {
				$source = escape_data($_REQUEST['source']);
				include 'var_source.php';
				$str .= "来源：{$problem_source[$source]}&nbsp;&nbsp;&nbsp;&nbsp;";
				if($first) {
					$clause .= "WHERE source = $source ";
					$first = 0;
				}
				else
					$query .= "AND source = $source ";
			}
			if($first)
				$str = '全部题目';
			
			/* Get result amount */
			$query = 'SELECT count(*) AS amount FROM problems ';
			$query .= $clause;
			$result = @mysqli_query($NDSOJ_dbc, $query);
			$row = mysqli_fetch_array($result);
			$amount = $row['amount'];
			mysqli_free_result($result);
			
			$str .= "（$amount 条结果）";
			echo '<p><strong>搜索：</strong>' . $str . '</p>';
			
			if($amount) {
				$query = 'SELECT id, name, difficulty FROM problems ';
				$query .= $clause . 'ORDER BY id DESC;';
				$result = @mysqli_query($NDSOJ_dbc, $query);
				if (!$result) {
					echo '呃，看起来出了点小问题…请尽快联系我们。我们的E-mail：hcc@shiyihcc.com';
					//echo mysqli_error($NDSOJ_dbc);
				}
				else {
					echo '<table>';
					echo "<tr class=\"tr_head\"><td class=\"td_problem\">题目</td><td>难度</td></tr>";
					while($row = mysqli_fetch_array($result)) {
						echo "<tr><td><a href=\"problem_show.php?id={$row['id']}\">[{$row['id']}]{$row['name']}</a></td>";
						echo "<td>{$row['difficulty']}</td></tr>\n";
					}
					echo '</table>';
					mysqli_free_result($result);
				}
			}
			?>
<?php include 'module_customsearch.php'; ?>
		</div>
		<div id="sidebar">
<?php include 'module_jumpbox.php'; ?>
		</div>
<?php include 'footer.php'; ?>
